<template>
  <div class="body" v-loading="loading">
    <div>
      <el-button type="info" size="small" icon="el-icon-back" @click="gotoList">&nbsp;返&nbsp;回&nbsp;</el-button>
      <el-button type="warning" size="small" icon="el-icon-download" @click="doDownload(this.header.billNo)">&nbsp;导&nbsp;出&nbsp;</el-button>
    </div>
    <el-form ref="form" :model="header" size="small" label-width="80px" :inline="true" class="header">
      <div class="audit_icon iconfont" v-if="header.status === 'audit'"></div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="单据编码">{{ header.billNo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="盘点人员">{{ header.createUserName }}</el-form-item>
        </el-col>
      </el-row>
      <br />
    </el-form>
    <el-table :data="dataList" class="detail" height="calc(100vh - 190px)">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="仓库" width="250" prop="stockNo">
        <template #default="scope">
          {{ getStockName(scope.row.stockNo) }}
        </template>
      </el-table-column>
      <el-table-column label="托盘编码" width="250" prop="trayNumber"></el-table-column>
      <el-table-column label="仓位" width="250" prop="locationNo"></el-table-column>
      <el-table-column label="数量" width="80" prop="count">
        <template #default="scope">
          {{ scope.row.count? Number(Math.abs(scope.row.count).toFixed(6)): '/'}}
        </template>
      </el-table-column>
      <el-table-column label="盘点状态" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'loss'? 'danger':( scope.row.status === 'save'? 'success': 'warning') ">{{ scope.row.status === 'loss'? '盘亏':( scope.row.status === 'save'? '已盘': '盘盈') }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getBoxNo } from '../../utils'
import BatchAddRowDialog from "../components/batchAddRowDialog.vue";

export default {
  components: {BatchAddRowDialog},
  data() {
    return {
      loading: false,
      header: {},
      dataList: [],
      stockList:[]
    }
  },
  async mounted() {
    await this.getStock()
    let billNo = this.$route.query.billNo
    if (billNo) {
      return this.getBill(billNo)
    } else {
      this.header.batchNo = dayjs().format('YYYYMMDDHHmmss')
    }
  },
  methods: {
    async doDownload(billNo) {
      this.loading = true
      try {
        let res = await this.callApi('download_tray_inventory_list', {billNo: billNo})
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    gotoList() {
      return this.$router.push('/tray/trayInventory')
    },
    async getBill(billNo) {
      this.loading = true
      try {
        let res = await this.callApi('get_tray_inventory_detail', { billNo })
        if (res.res) {
          this.dataList = res.data
          this.header = res.header || {}
        }
      } finally {
        this.loading = false
      }
    },
    async doDelete() {
      try {
        await this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('delete_prdcol_bill', { billNo: this.header.billNo })
        if (!res.res) {
          return
        }
        return this.gotoList()
      } finally {
        this.loading = false
      }
    },
    async doAudit() {
      this.loading = true
      try {
        let res = await this.callApi('audit_prd_task_bill', { billNo: this.header.billNo })
        if (!res.res) {
          return
        }
        this.header.status = res.status
        this.$message.success('审核完成!')
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
  }
}
</script>

<style lang="scss" scoped>
.body {
  padding: 20px 0;
  .header {
    margin-top: 20px;
    position: relative;
    .header_row {
      display: flex;
      >div {
        flex-shrink: 0;
      }
      :deep(.produceDate) {
        .el-input {
          width: 100%!important;
        }
      }
    }
    .audit_icon {
      position: absolute;
      top: -10px;
      left: 60px;
      font-size: 48px;
      color: red;
    }
    .material {
      width: 100%;
    }
    .p3 {
      :deep(.el-form-item__content) {
        display: flex;
        .comment {
          margin-left: 10px;
          word-break: keep-all;
        }
      }
    }
  }
  .detail_header {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    .stat {
      display: flex;
      font-size: 15px;
      >div {
        display: flex;
        align-items: baseline;
        line-height: 32px;
        .label {
          text-align: right;
          margin-right: 5px;
          color: #aaa;
        }
        .value {
          min-width: 50px;
          margin-right: 10px;
          color: #666;
        }
      }
    }
  }
}
</style>
